package top.truehaku.generator.code.domain.model.column;

import lombok.Getter;
import lombok.Setter;
import org.springframework.util.CollectionUtils;
import top.truehaku.generator.common.domain.Entity;
import top.truehaku.generator.common.enums.JavaType;
import top.truehaku.generator.common.util.StringUtils;
import java.util.List;

@Getter@Setter
public class Column implements Entity<Column> {

    private ColumnId columnId;

    private ColumnType columnType;

    private ColumnComment columnComment;

    private Boolean nullable;

    private Boolean primaryKey;

    private Boolean uniqueKey;

    private Boolean autoIncrement;

    private Integer ordinalPosition;

    @Getter
    private Boolean parentIncluded;

    private List<Column> combineColumns;

    public Column(ColumnId columnId, ColumnType columnType, ColumnComment columnComment, Boolean nullable, Boolean primaryKey,Boolean uniqueKey, Boolean autoIncrement, Integer ordinalPosition) {
        this.columnId = columnId;
        this.columnType = columnType;
        this.columnComment = columnComment;
        this.nullable = nullable;
        this.primaryKey = primaryKey;
        this.uniqueKey = uniqueKey;
        this.autoIncrement = autoIncrement;
        this.ordinalPosition = ordinalPosition;
        this.parentIncluded = Boolean.FALSE;
    }


    @Override
    public boolean sameIdentityAs(Column other) {
        return other!=null&&this.columnId.equals(other.columnId);
    }

    public String getFieldName(){
        return StringUtils.toCamelCase(this.getColumnId().getColumnName(),false);
    }
    public String getFieldCapitalizeName(){
        return StringUtils.toCamelCase(this.getColumnId().getColumnName(),true);
    }
    public void setParentIncluded(List<String> parentFields){
        if (!CollectionUtils.isEmpty(parentFields)&&parentFields.contains(this.getFieldName())){
            this.parentIncluded = Boolean.TRUE;
        }
    }

    public String getCondition(){
        return uniqueKey?"eq":getColumnComment().getCondition();
    }

    /**
     * 是否为查询条件
     * @return
     */
    public Boolean isCondition(){
        return uniqueKey||getColumnComment().isCondition();
    }

    public static void main(String[] args) {
        Column column = new Column(
                new ColumnId("test", "demo", "name")
                , new ColumnType("varchar(256)")
                , new ColumnComment("这是注释")
                , true
                , true
                , true
                , true
                , 1
        );
        JavaType javaType = column.getColumnType().getJavaType();
        System.out.println(javaType.equals(JavaType.STRING));

    }

}
